# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html

#import MySQLdb
#import MySQLdb.cursors
from kickstart import items
from kickstart import settings
from twisted.enterprise import adbapi
import pymongo
from scrapy.contrib.pipeline.images import ImagesPipeline
from scrapy.exceptions import DropItem

class CtripPipeline(object):
    def __init__(self):
        self.serverIP = '127.0.0.1'
        self.serverPort = 54545
        self.serverDB = 'ctrip'
        self.client = pymongo.MongoClient(self.serverIP, self.serverPort)
        self.db = self.client[self.serverDB]
        self.html = self.db['html']
        self.pic = self.db['pic']
        self.city = self.db['city']
        self.hotel = self.db['hotel']
        self.comment = self.db['comment']
        self.youji = self.db['youji']

    def addHTML(self, item):
        if self.html.find_one({'url': item['url']}):
            return
        self.html.save(dict(item))

    def addPIC(self, pic):
        if self.pic.find_one({'url': pic['url']}):
            return
        self.pic.save(dict(pic))
           
    def addCity(self, city):
        if self.city.find_one({'cid': city['cid']}):
            return
        self.city.save(dict(city))
    
    def addHotel(self, hotel):
        if self.hotel.find_one({'id': hotel['id']}):
            return
        self.hotel.save(dict(hotel))

    def addComment(self, comment):
        if self.comment.find_one({'hotelid': comment['hotelid'], 'uname':
            comment['uname'], 'date': comment['date'] }):
            return
        self.comment.save(dict(comment))

    def addSight(self, sight):
        if self.sight.find_one({'cid': sight['cid'], 'sid': sight['sid']}):
            return
        self.sight.save(dict(sight))
    
    def addSightComment(self, comment):
        if self.comment.find_one({'cid': comment['cid'], 'sightid': comment['sightid'], 'uname': comment['uname'], 'traveldate': comment['traveldate'], 'commentdate': comment['commentdate']}):
            return
        self.comment.save(dict(comment))

    def addYouji(self, youji):
        if self.youji.find_one({'url': youji['url']}):
            return
        self.youji.save(dict(youji))
    def process_item(self, item, spider):
        if isinstance(item, items.CtripCity):
            self.addCity(item)
        elif isinstance(item, items.CtripHotel):
            self.addHotel(item)
        elif isinstance(item, items.CtripHotelComment):
            self.addComment(item)
        elif isinstance(item, items.CtripCityHTML):
            self.addHTML(item)
        elif isinstance(item, items.CtripHotelHTML):
            self.addHTML(item)
        elif isinstance(item, items.CtripHotelPIC):
            self.addPIC(item)
        elif isinstance(item, items.CtripHotelCommentHTML):
            self.addHTML(item)
        elif isinstance(item, items.CtripCitySightHTML):
            self.addHTML(item)
        elif isinstance(item, items.CtripSight):
            self.addSight(item)
        elif isinstance(item, items.CtripSightHTML):
            self.addHTML(item)
        elif isinstance(item, items.CtripSightComment):
            self.addSightComment(item)
        elif isinstance(item, items.CtripSightCommentHTML):
            self.addHTML(item)
        elif isinstance(item, items.CtripSightPic):
            self.addPIC(item)
        elif isinstance(item, items.CtripYouji):
            self.addYouji(item)
        else:
            print "can not deal item: ", item
